<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>tbox协程使用之切换与等待</title>
    <meta name="description" content="tbox的协程实现，是stackfull模式的，需要指定独立堆栈和协程函数，目前暂时还不能像golang那样实现堆栈的动态增长，之后会对其进行支持。目前提供下面一些功能特性：1. 提供yield切换调度支持，这个是必须的哈2. 提供suspend(挂起)/resume(恢复)协程接口，不同于yield的是，被su...">

    
    <meta name="keywords" content="tbox,协程,切换,调度器,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2016/10/29/coroutine-switch/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>tbox协程使用之切换与等待</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2016-10-29
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#tbox" title="Category: tbox" rel="category">tbox</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a-->
        <a href="/cn/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a>&nbsp;
    
        <!--a href="/tag/#%E5%8D%8F%E7%A8%8B" title="Tag: 协程" rel="tag">协程</a-->
        <a href="/cn/tag/#协程" title="Tag: 协程" rel="tag">协程</a>&nbsp;
    
        <!--a href="/tag/#%E5%88%87%E6%8D%A2" title="Tag: 切换" rel="tag">切换</a-->
        <a href="/cn/tag/#切换" title="Tag: 切换" rel="tag">切换</a>&nbsp;
    
        <!--a href="/tag/#%E8%B0%83%E5%BA%A6%E5%99%A8" title="Tag: 调度器" rel="tag">调度器</a-->
        <a href="/cn/tag/#调度器" title="Tag: 调度器" rel="tag">调度器</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p>tbox的协程实现，是stackfull模式的，需要指定独立堆栈和协程函数，目前暂时还不能像golang那样实现堆栈的动态增长，之后会对其进行支持。</p>

<p>目前提供下面一些功能特性：</p>

<div class="highlighter-rouge"><pre class="highlight"><code>1. 提供yield切换调度支持，这个是必须的哈
2. 提供suspend(挂起)/resume(恢复)协程接口，不同于yield的是，被suspend后，如果不显示调用resume恢复它，是永远不会被调度到的
3. 提供sleep等待接口支持
4. 提供io调度支持，支持socket等io等待（内部使用epoll, poll, kqueue, select, poll等接口调度）
5. 原生支持stream，socket，http等模块的协程支持，可与线程进行无缝切换
6. 提供channel同道，进行协程间交互通信
7. 提供lock，semaphore等接口
</code></pre>
</div>

<p>这里主要讲讲，基础的切换调度如何使用。。</p>

<h4 id="yield切换">yield切换</h4>

<p>这个的使用非常简单，直接上代码吧，嘿嘿。。</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code><span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">switchfunc</span><span class="p">(</span><span class="n">tb_cpointer_t</span> <span class="n">priv</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// 获取传入的参数
</span>    <span class="n">tb_size_t</span> <span class="n">count</span> <span class="o">=</span> <span class="p">(</span><span class="n">tb_size_t</span><span class="p">)</span><span class="n">priv</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">count</span><span class="o">--</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 打印当前协程id
</span>        <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"[coroutine: %p]: %lu"</span><span class="p">,</span> <span class="n">tb_coroutine_self</span><span class="p">(),</span> <span class="n">count</span><span class="p">);</span>

        <span class="c1">// 让出当前协程，进行切换
</span>        <span class="n">tb_coroutine_yield</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">}</span>
<span class="n">tb_int_t</span> <span class="nf">main</span><span class="p">(</span><span class="n">tb_int_t</span> <span class="n">argc</span><span class="p">,</span> <span class="n">tb_char_t</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// 初始化tbox
</span>    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">tb_init</span><span class="p">(</span><span class="n">tb_null</span><span class="p">,</span> <span class="n">tb_null</span><span class="p">))</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>

    <span class="c1">// 初始化一个调度器实例
</span>    <span class="n">tb_co_scheduler_ref_t</span> <span class="n">scheduler</span> <span class="o">=</span> <span class="n">tb_co_scheduler_init</span><span class="p">();</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">scheduler</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 开启一个协程，传递switchfunc切换函数和参数10，最后一个参数指定栈大小，传0使用默认值
</span>        <span class="n">tb_coroutine_start</span><span class="p">(</span><span class="n">scheduler</span><span class="p">,</span> <span class="n">switchfunc</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">10</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>

        <span class="c1">// 开启协程，使用指定的栈大小：8192
</span>        <span class="n">tb_coroutine_start</span><span class="p">(</span><span class="n">scheduler</span><span class="p">,</span> <span class="n">switchfunc</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">10</span><span class="p">,</span> <span class="mi">8192</span><span class="p">);</span>

        <span class="cm">/* 运行调用，开始运行里面的所有协程
         *
         * 第二个参数指定是否启用独占模式，这个稍后细讲
         */</span>
        <span class="n">tb_co_scheduler_loop</span><span class="p">(</span><span class="n">scheduler</span><span class="p">,</span> <span class="n">tb_true</span><span class="p">);</span>

        <span class="c1">// 退出调度器
</span>        <span class="n">tb_co_scheduler_exit</span><span class="p">(</span><span class="n">scheduler</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="c1">// 退出tbox
</span>    <span class="n">tb_exit</span><span class="p">();</span>
<span class="p">}</span>
</code></pre>
</div>

<p>所有协程执行完后，就会从loop调用处返回，当然在协程函数内部也是可以嵌套开启新协程的</p>

<p>这个时候第一个参数就不需要显示指定scheduler了，传<code class="highlighter-rouge">tb_null</code>表示在当前调度器环境中开新协程，例如：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code><span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">switchfunc2</span><span class="p">(</span><span class="n">tb_cpointer_t</span> <span class="n">priv</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// ..
</span><span class="p">}</span>
<span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">switchfunc</span><span class="p">(</span><span class="n">tb_cpointer_t</span> <span class="n">priv</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// 在当前协程函数内，开启一个新协程    
</span>    <span class="n">tb_coroutine_start</span><span class="p">(</span><span class="n">tb_null</span><span class="p">,</span> <span class="n">switchfunc2</span><span class="p">,</span> <span class="n">tb_null</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>

    <span class="c1">// 获取传入的参数
</span>    <span class="n">tb_size_t</span> <span class="n">count</span> <span class="o">=</span> <span class="p">(</span><span class="n">tb_size_t</span><span class="p">)</span><span class="n">priv</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">count</span><span class="o">--</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 让出当前协程，进行切换
</span>        <span class="n">tb_coroutine_yield</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>

<h4 id="独占模式">独占模式</h4>

<p>上面的代码中提到的独占模式，需要特殊说明下，一般情况下，传<code class="highlighter-rouge">tb_false</code>到loop()中，不启用此模式是最为稳妥的。</p>

<p>因为这样每个scheduler对应的线程都会在Tls中维护自己的scheduler引用，使得协程中所有操作，都回去访问当前线程tls对应的独立scheduler，互不干涉。</p>

<p>这个在存在多个scheduler的情况，尤为重要，当时这样多少会有些tls操作上的性能损耗，像server端一般只有一个scheduler的情况下，就没必要放到独立tls中去了</p>

<p>可以传入tb_true启用独占模式，告诉tb_co_scheduler_loop()，我当前不需要tls维护，只需要一个全局scheduler变量来维护就行了</p>

<p>这样的话，性能会提升一些，因此在只有一个scheduler存在的情况下，启用独占效率会高些。。</p>

<h4 id="sleep等待">sleep等待</h4>

<p>tbox的<code class="highlighter-rouge">tb_sleep</code>和<code class="highlighter-rouge">tb_msleep()</code>接口，是可以原生支持协程的，在协程外就是线程等待，在协程内就是协程等待。</p>

<p>当然也可以直接使用协程的接口：<code class="highlighter-rouge">tb_coroutine_sleep()</code></p>

<p>例如：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code><span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">sleepfunc</span><span class="p">(</span><span class="n">tb_cpointer_t</span> <span class="n">priv</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">while</span> <span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 等待10ms，切换到其他协程，直到10ms后才会切换回来继续执行
</span>        <span class="n">tb_msleep</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>

<h4 id="resumesuspend接口">resume/suspend接口</h4>

<p>挂起域恢复，跟yield的区别就是，被suspend挂起的协程，默认是不会被切换调度回来的，除非执行resume恢复它。</p>

<p>因此这两个接口是成对使用的，像sleep，lock和semaphore的内部实现，也是基于此套接口。</p>

<p>这两个接口还有个功能，就是可以在两个协程间，更加快速方便的传递一些参数数据，进行交互，而不需要channel支持。。</p>

<p>例如：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code><span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">resumefunc</span><span class="p">(</span><span class="n">tb_cpointer_t</span> <span class="n">priv</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// 获取suspend协程引用 
</span>    <span class="n">tb_coroutine_ref_t</span> <span class="n">coroutine</span> <span class="o">=</span> <span class="p">(</span><span class="n">tb_coroutine_ref_t</span><span class="p">)</span><span class="n">priv</span><span class="p">;</span>

    <span class="cm">/* 恢复suspend协程，传递参数"hello suspend!"给suspend()作为其返回值
     *
     * retval为suspend()中传入的参数："hello resume!"
     */</span>
    <span class="n">tb_char_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">retval</span> <span class="o">=</span> <span class="n">tb_coroutine_resume</span><span class="p">(</span><span class="n">coroutine</span><span class="p">,</span> <span class="s">"hello suspend!"</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">suspendfunc</span><span class="p">(</span><span class="n">tb_cpointer_t</span> <span class="n">priv</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// 开启一个恢复协程，传入当前协程引用
</span>    <span class="n">tb_coroutine_start</span><span class="p">(</span><span class="n">tb_null</span><span class="p">,</span> <span class="n">resumefunc</span><span class="p">,</span> <span class="n">tb_coroutine_self</span><span class="p">(),</span> <span class="mi">0</span><span class="p">);</span>

    <span class="cm">/* 挂起当前协程，传递参数"hello resume!"给resume()作为其返回值
     *
     * retval为resume()中传入的参数："hello suspend!"
     */</span>
    <span class="n">tb_char_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">retval</span> <span class="o">=</span> <span class="n">tb_coroutine_suspend</span><span class="p">(</span><span class="s">"hello resume!"</span><span class="p">);</span>
<span class="p">}</span>
</code></pre>
</div>

<p>当然，如果不需要交互数据，那么只需要传<code class="highlighter-rouge">tb_null</code>就行了。。</p>

        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
                    
                    <h2 id="english">English</h2>
                    <ul>
                    
                    <li class="relatedPost">
                        <a href="/2016/10/29/coroutine-switch/">Some examples for coroutine (tbox)
                        
                        </a>
                    </li>
                    
                    
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        
            </ul>
        

        
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2016/10/28/coroutine-context/">协程分析之context上下文切换</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2016/10/30/update-v1.6.0/">tbox v1.6.0 发布，提供跨平台协程支持!</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
